<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="baidu-site-verification" content="5pQM9dB1uO" />
<meta name="google-site-verification" content="dY4Uxh6geXuXnQR_3mASUgoRCnXG4gmKcISzY0_3vNM" />
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.chenkl.vip","root":"/","scheme":"Mist","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="在学习过程中，可能会有很多人、很多文章告诉你面向对象很重要，原型很重要，可是却很少有人告诉你，面向对象所有的重点难点，几乎都与函数息息相关。">
<meta property="og:type" content="article">
<meta property="og:title" content="前端基础进阶学习(八)--函数与函数式编程">
<meta property="og:url" content="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%85%AB)--%E5%87%BD%E6%95%B0%E4%B8%8E%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/index.html">
<meta property="og:site_name" content="小小前端搬运工">
<meta property="og:description" content="在学习过程中，可能会有很多人、很多文章告诉你面向对象很重要，原型很重要，可是却很少有人告诉你，面向对象所有的重点难点，几乎都与函数息息相关。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-27T09:45:01.000Z">
<meta property="article:modified_time" content="2020-08-22T02:51:57.439Z">
<meta property="article:author" content="Archer">
<meta property="article:tag" content="javaScript">
<meta property="article:tag" content="前端进阶">
<meta property="article:tag" content="函数与函数式编程">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%85%AB)--%E5%87%BD%E6%95%B0%E4%B8%8E%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端基础进阶学习(八)--函数与函数式编程 | 小小前端搬运工</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="小小前端搬运工" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小小前端搬运工</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</h1>
      
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">49</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">7</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">28</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="search-pop-overlay">
  <div class="popup search-popup">
      <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

  </div>
</div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%85%AB)--%E5%87%BD%E6%95%B0%E4%B8%8E%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Archer">
      <meta itemprop="description" content="种一棵树最好的时间是十年前，其次就是现在。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小小前端搬运工">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          前端基础进阶学习(八)--函数与函数式编程
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-07-27 09:45:01" itemprop="dateCreated datePublished" datetime="2020-07-27T09:45:01Z">2020-07-27</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/javaScript/" itemprop="url" rel="index"><span itemprop="name">javaScript</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>在学习过程中，可能会有很多人、很多文章告诉你面向对象很重要，原型很重要，可是却很少有人告诉你，面向对象所有的重点难点，几乎都与函数息息相关。</p>
</blockquote>
<a id="more"></a>

<h2 id="函数声明、函数表达式、匿名函数与自执行函数"><a href="#函数声明、函数表达式、匿名函数与自执行函数" class="headerlink" title="函数声明、函数表达式、匿名函数与自执行函数"></a>函数声明、函数表达式、匿名函数与自执行函数</h2><blockquote>
<p>关于函数在实际开发中的应用，大体可以总结为函数声明、函数表达式、匿名函数、自执行函数。</p>
</blockquote>
<h3 id="函数声明"><a href="#函数声明" class="headerlink" title="函数声明"></a>函数声明</h3><blockquote>
<p>在JavaScript中，有两种声明方式，一个是使用<code>var/let/const</code>的变量声明，另一个是使用<code>function</code>的函数声明。</p>
</blockquote>
<blockquote>
<p>我们知道，变量对象的创建过程中，函数声明比变量声明具有更为优先的执行顺序，即我们常常提到的函数声明提前。因此我们在执行上下文中，无论在什么位置声明函数，都可以在同一个执行上下文中直接使用该函数。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">fn()</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">fn</span></span>() &#123;</span><br><span class="line">  console.log(<span class="string">'function'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="函数表达式"><a href="#函数表达式" class="headerlink" title="函数表达式"></a>函数表达式</h3><blockquote>
<p>与函数声明不同，函数表达式使用了<code>var/let/const</code>进行声明，那么我们在确认他是否可以正确使用的时候就必须依照<code>var/let/const</code>的规则进行判断，即变量声明。我们知道使用var进行变量声明，其实是进行了两步操作。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 变量声明</span><br><span class="line">var a = 20</span><br><span class="line"></span><br><span class="line">// 实际执行顺序</span><br><span class="line">var a = undefined // 变量声明，初始值undefined，变量提升，提升顺序次于<span class="keyword">function</span>声明</span><br><span class="line">a = 20 // 变量赋值，该操作不会提升</span><br></pre></td></tr></table></figure>

<blockquote>
<p>同样的道理，当我们使用变量声明的方式来声明函数时，就是我们常常说的函数表达式。函数表达的提升方式与变量声明一致。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">fn() // 报错</span><br><span class="line"></span><br><span class="line">var fn = <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  console.log(<span class="string">'function'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>上例的执行顺序为：</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var fn = undefined // 变量提升</span><br><span class="line"></span><br><span class="line">fn() // 执行报错</span><br><span class="line"></span><br><span class="line">fn = <span class="function"><span class="title">function</span></span>() &#123; // 赋值操作，此时将后边函数的引用赋值给fn</span><br><span class="line">  console.log(<span class="string">'function'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<div class="note info">
            <p>因此，由于声明方式的不同，导致了函数声明与函数表达式在使用上的一些差异需要我们注意，除此之外，这两种形式的函数在使用上并无不同。</p>
          </div>

<h3 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h3><blockquote>
<p>匿名函数，顾名思义，就是指的没有被显示进行赋值操作的函数。它的使用场景，多作为一个参数传入另外一个函数中。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var a = 20</span><br><span class="line">var fn = <span class="keyword">function</span>(bar, num) &#123;</span><br><span class="line">  <span class="built_in">return</span> bar() + num</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  <span class="built_in">return</span> a</span><br><span class="line">&#125;, 20)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在上面的例子中，fn的第一个参数传入了一个匿名函数。虽然该匿名函数没有显示的进行赋值操作，我们没有办法在外部执行上下文中引用到它，但是在fn函数内部，我们将该匿名函数赋值给了变量bar，保存在了fn变量对象的arguments对象中。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">// 变量对象在fn上下文执行过程中的创建阶段</span><br><span class="line">OV(fn) = &#123;</span><br><span class="line">  arguments: &#123;</span><br><span class="line">    bar: undefined,</span><br><span class="line">    num: undefined,</span><br><span class="line">    length: 2</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 变量对象在fn上下文执行过程中的执行阶段</span><br><span class="line">// 变量对象变为活跃对象，并完成赋值操作与执行可执行代码</span><br><span class="line">VO -&gt; AO</span><br><span class="line"></span><br><span class="line">AO(fn) = &#123;</span><br><span class="line">  arguments: &#123;</span><br><span class="line">    bar: <span class="function"><span class="title">function</span></span>() &#123; <span class="built_in">return</span> a &#125;,</span><br><span class="line">    num: 20,</span><br><span class="line">    length: 2</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>由于匿名函数传入另一个函数之后，最终会在另一个函数中执行，因此我们也尝尝称这个匿名函数为回调函数。</p>
</blockquote>
<h2 id="函数自执行与块级作用域"><a href="#函数自执行与块级作用域" class="headerlink" title="函数自执行与块级作用域"></a>函数自执行与块级作用域</h2><blockquote>
<p>在ES5中，没有块级作用域，因此我们常常使用函数自执行的方式来模仿块级作用域，这样就提供了一个独立的执行上下文，结合闭包，就为模块化提供了基础。而函数自执行，其实是匿名函数的一种应用。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  // ...</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>一个模块往往可以包括：私有变量、私有方法、公有变量、公有方法。</p>
</blockquote>
<blockquote>
<p>根据作用域链的单向访问，外面可能很容易知道在这个独立的模块中，外部执行环境是无法访问内部的任何变量与方法的，因此我们可以很容易的创建属于这个模块的私有变量与私有方法。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  // 私有变量</span><br><span class="line">  var age = 20</span><br><span class="line">  var name = <span class="string">'haha'</span></span><br><span class="line">  </span><br><span class="line">  // 私有方法</span><br><span class="line">  <span class="keyword">function</span> <span class="function"><span class="title">getName</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> <span class="string">'your name is '</span> + name</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>但是公有方法和变量应该怎么办？我们可以利用闭包，我们可以访问到执行上下文内部的变量和方法，因此，只需要根据闭包的定义，创建一个闭包，将你认为需要公开的变量和方法开放出来即可。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  // 私有变量</span><br><span class="line">  var age = 20</span><br><span class="line">  var name = <span class="string">'haha'</span></span><br><span class="line">  </span><br><span class="line">  // 私有方法</span><br><span class="line">  <span class="keyword">function</span> <span class="function"><span class="title">getName</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> <span class="string">'your name is '</span> + name</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 公有方法</span><br><span class="line">  <span class="keyword">function</span> <span class="function"><span class="title">getAge</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> age</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 讲引用保存在外部执行环境的变量，形成闭包，防止该执行环境被垃圾回收</span><br><span class="line">  window.getAge = getAge</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>

<blockquote>
<p>为了进一步了解闭包，我们来看看jQuery中，是如何利用模块与闭包的。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">// 使用函数自执行的方式创建模块</span><br><span class="line">(<span class="keyword">function</span>(window, undefined) &#123;</span><br><span class="line">  </span><br><span class="line">  // 声明jQuery构造函数</span><br><span class="line">  var jQuery = <span class="keyword">function</span>(name) &#123;</span><br><span class="line">    </span><br><span class="line">    // 主动在构造函数中，返回一个jQuery实例</span><br><span class="line">    <span class="built_in">return</span> new jQuery.fn.init(name)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 添加原型方法</span><br><span class="line">  jQuery.prototype = jQuery.fn = &#123;</span><br><span class="line">    constructor: jQuery,</span><br><span class="line">    init: <span class="function"><span class="title">function</span></span>() &#123;...&#125;,</span><br><span class="line">    css: <span class="function"><span class="title">function</span></span>() &#123;...&#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  jQuery.fn.init.prototype = jQuery.fn</span><br><span class="line">  </span><br><span class="line">  // 将jQuery改名为$，并将引用保存在window，形成闭包，对外开发jQuery构造函数，这样我们就可以访问所有挂载在jQuery原型上的方法了。</span><br><span class="line">  window.jQuery = window.$ = jQuery</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">// 在使用时，直接执行了构造函数，因为在jQuery的构造函数中通过一些手段，返回的是jQuery的实例，所以我们就不用再每次用的时候自己new一个实例了。</span><br><span class="line">$(<span class="string">'#div'</span>)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>接下来，我们来看下一个高级的，非常有用的模块的应用。当我们的项目越来越大，那么需要保存的数据与状态就越来越多，因此，我们需要一个专门的模块来维护这些数据，这个时候，一个叫做状态管理器的东西就应运而生了。对于状态管理器，最出名的，非redux莫属了。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">// 自执行创建模块</span><br><span class="line">(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  // states 结构预览</span><br><span class="line">  // states = &#123;</span><br><span class="line">    a: 1,</span><br><span class="line">    b: 2,</span><br><span class="line">    m: 30,</span><br><span class="line">    o: &#123;&#125;</span><br><span class="line">  &#125;</span><br><span class="line">  var states = &#123;&#125; // 私有变量，用来存储状态与数据</span><br><span class="line">  </span><br><span class="line">  // 判断数据类型</span><br><span class="line">  <span class="keyword">function</span> <span class="built_in">type</span>(elem) &#123;</span><br><span class="line">    <span class="keyword">if</span> (elem === null) &#123;</span><br><span class="line">      <span class="built_in">return</span> elem + <span class="string">''</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">return</span> toString.call(elem).replace(/[\[\]]/g, <span class="string">''</span>).split(<span class="string">' '</span>)[1].toLowerCase()</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * @Param name 属性名</span><br><span class="line">   * @Description 通过属性名获取保存在states中的值</span><br><span class="line">   */</span><br><span class="line">  <span class="keyword">function</span> get(name) &#123;</span><br><span class="line">    <span class="built_in">return</span> states[name] ? states[name] : <span class="string">''</span></span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">function</span> <span class="function"><span class="title">getStates</span></span>() &#123;</span><br><span class="line">    <span class="built_in">return</span> states</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  /*</span><br><span class="line">   * @param options &#123;object&#125; 键值对</span><br><span class="line">   * @param target &#123;object&#125; 属性值为对象的属性，只在函数实现时递归中传入</span><br><span class="line">   * @desc 通过传入键值对的方式修改state树，使用方式与小程序的data或者react中的setStates类似</span><br><span class="line">  */</span><br><span class="line">  <span class="keyword">function</span> <span class="built_in">set</span>(options, target) &#123;</span><br><span class="line">    var keys = Object.keys(options)</span><br><span class="line">    var o = target ? target : states</span><br><span class="line">    </span><br><span class="line">    keys.map(<span class="keyword">function</span>(item) &#123;</span><br><span class="line">      <span class="keyword">if</span> (typeof o[item] === <span class="string">'undefined'</span>) &#123;</span><br><span class="line">        o[item] = options[item]</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="built_in">type</span>(o[item]) === <span class="string">'object'</span> ? <span class="built_in">set</span>(options[item], o[item]) : o[item] = options[item]</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">return</span> item</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  // 对外提供接口</span><br><span class="line">  window.get = get</span><br><span class="line">  window.set = <span class="built_in">set</span></span><br><span class="line">  window.getStates = getStates</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">// 具体使用如下</span><br><span class="line"><span class="built_in">set</span>(&#123; a: 20 &#125;) // 保存属性a</span><br><span class="line"><span class="built_in">set</span>(&#123; b: 100 &#125;) // 保存属性b</span><br><span class="line"><span class="built_in">set</span>(&#123; c: 10 &#125;) // 保存属性c</span><br><span class="line"></span><br><span class="line">// 保存属性o，它的值为一个对象</span><br><span class="line"><span class="built_in">set</span>(&#123;</span><br><span class="line">  o: &#123;</span><br><span class="line">    m: 10,</span><br><span class="line">    n: 20</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">// 修改对象</span><br><span class="line"><span class="built_in">set</span>(&#123;</span><br><span class="line">  o: &#123;</span><br><span class="line">    m: 1000</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">// 给对象o中增加一个c属性</span><br><span class="line"><span class="built_in">set</span>(&#123;</span><br><span class="line">  o: &#123;</span><br><span class="line">    c: 100</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">console.log(getStates())</span><br></pre></td></tr></table></figure>

<h2 id="函数参数传递方式：按值传递"><a href="#函数参数传递方式：按值传递" class="headerlink" title="函数参数传递方式：按值传递"></a>函数参数传递方式：按值传递</h2><blockquote>
<p>基本数据类型复制，是值直接发生了复制，因此改变后，各自相互不影响。但是引用数据类型的复制，是保存在变量对象中的引用发生了复制，因此复制之后的这两个引用实际访问的是同一个堆内存中的值。当改变其中一个时，另外一个自然也被改变。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var a = 20</span><br><span class="line">var b = a</span><br><span class="line">b = 10</span><br><span class="line">console.log(a) // 20</span><br><span class="line"></span><br><span class="line">var m = &#123; a: 1, b: 2 &#125;</span><br><span class="line">var n = m</span><br><span class="line">n.a = 5</span><br><span class="line">console.log(m.a) // 5</span><br></pre></td></tr></table></figure>

<blockquote>
<p>当值作为函数的参数传递进入函数内部时，也有同样的差异。我们知道，函数的参数在进入函数后，实际是被保存在了函数的变量对象中，因此，这个时候相当于发生了一次复制。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var a = 20</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> fn(a) &#123;</span><br><span class="line">  a = a + 10</span><br><span class="line">  <span class="built_in">return</span> a</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn(a)</span><br><span class="line">console.log(a)</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var a = &#123; m: 10, n: 20 &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> fn(a) &#123;</span><br><span class="line">  a.m = 20</span><br><span class="line">  <span class="built_in">return</span> a</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fn(a)</span><br><span class="line">console.log(a) // &#123; m: 20, n: 20 &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>正是由于这样的不同，导致了许多人在理解函数参数的传递方式时，就有许多困惑。到底是按值传递还是按引用传递？实际上结论仍然是按值传递，只不过当我们期望传递一个引用类型时，真正传递的，只是这个引用保存在变量对象中的引用而已。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var person = &#123;</span><br><span class="line">  name: <span class="string">'haha'</span>,</span><br><span class="line">  age: 20</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">fucntion setName(obj) &#123; // 传入一个引用</span><br><span class="line">  obj = &#123;&#125; // 将传入的引用指向另外的值</span><br><span class="line">  obj.name = <span class="string">'heihei'</span> // 修改引用的name属性</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">setName(person)</span><br><span class="line">console.log(person.name) // haha未被改变</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在上面的例子中，如果person是按引用传递，那么person就会自动被修改为指向其name属性值为heihei的新对象。但是我们从结果中看到，person对象并未发生任何改变，因此只是在函数内部引用被修改而已。</p>
</blockquote>
<h2 id="函数式编程"><a href="#函数式编程" class="headerlink" title="函数式编程"></a>函数式编程</h2><blockquote>
<p>虽然JavaScript并不是一门纯函数式编程的语言，但是它使用了许多函数式编程的特性。因此了解这些特性可以让我们更加的了解自己写的代码。</p>
</blockquote>
<blockquote>
<p>当我们想要使用一个函数时，通常情况下其实就是想要将一些功能，逻辑等封装起来。我们通常通过函数封装来完成一件事情，例如：想要计算任意三个数的和。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> add(a, b, c) &#123;</span><br><span class="line">  <span class="built_in">return</span> a + b + c</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">add(1 ,2, 3) //6</span><br></pre></td></tr></table></figure>

<blockquote>
<p>当然，当想要做的事情比较简单时，可能还看不出来封装成为函数之后带来的便利。如果我们想要做的事情稍微复杂一点呢。例如我想要计算一个数组中的所有子项目中的和。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> mergeArr(arr) &#123;</span><br><span class="line">  var result = 0</span><br><span class="line">  <span class="keyword">for</span> (var i = 0; i &lt; arr.length; i ++) &#123;</span><br><span class="line">    result += arr[i]</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">return</span> result</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>如果不通过函数封装的方式，那么再每次想要实现这个功能时，就不得不重新使用一次for循环，这样的后果就是我们代码充斥着越来越多重复代码。而封装之后，当我们想要再次做这个事情的时候，只需要调用就可以了。</p>
</blockquote>
<blockquote>
<p>当我们想要去封装一个函数时，如果做才是最佳实践？</p>
</blockquote>
<blockquote>
<p>我们在初学时，往往会不由自主的使用命令式编程的风格来完成我们想要干的事情。因为命令式变成更加简单，直白。例如我们现在有一个数组，<code>array = [1, 3, &#39;h&#39;, 5, &#39;m&#39;, &#39;4&#39;]</code>，现在想要找出这个数组中的所有类型为number的子项。当我们使用命令式变成思维时，可能就会直接这样做。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var array = [1, 3, <span class="string">'h'</span>, 5, <span class="string">'m'</span>, <span class="string">'4'</span>]</span><br><span class="line">var res = []</span><br><span class="line"><span class="keyword">for</span> (var i = 0; i &lt; array.length; i ++) &#123;</span><br><span class="line">  <span class="keyword">if</span> (typeof array[i] === <span class="string">'number'</span>) &#123;</span><br><span class="line">    res.push(array[i])</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在这种实现方式中，我们平铺直叙的实现了我们的目的。这样做的问题在于，当我们在另外的时刻，想要找出另外一个数组中所有的子项时，我们不得不把同样的逻辑再写一次。当出现次数变多时，我们的代码也变得更加糟糕且难以维护。</p>
</blockquote>
<blockquote>
<p>而函数式变成的思想则建议我们将这种会多次出现的功能封装起来以备用。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> getNumbers(array) &#123;</span><br><span class="line">  var res = []</span><br><span class="line">  array.forEach(<span class="keyword">function</span>(item) &#123;</span><br><span class="line">    <span class="keyword">if</span> (typeof item === <span class="string">'number'</span>) &#123;</span><br><span class="line">      res.push(item)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="built_in">return</span> res</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 以上是我们的封装，以下是功能实现</span><br><span class="line">var array = [1, 3, <span class="string">'h'</span>, 5, <span class="string">'m'</span>, <span class="string">'4'</span>]</span><br><span class="line">var res = getNumbers(array)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>函数式变成思维还具有以下几个特征</p>
</blockquote>
<ul>
<li>函数是第一等公民</li>
</ul>
<blockquote>
<p>指的是函数与其他数据类型一样，处于平等地位，可以赋值给其他变量，也可以作为参数，传入另一个函数，或者作为别的函数的返回值。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var a = <span class="keyword">function</span> <span class="function"><span class="title">foo</span></span>() &#123;&#125; // 赋值</span><br><span class="line"><span class="keyword">function</span> fn(<span class="function"><span class="title">function</span></span>() &#123;&#125;, num) &#123;&#125; // 函数作为参数</span><br><span class="line"></span><br><span class="line">// 函数作为返回值</span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">var</span></span>() &#123;</span><br><span class="line">  <span class="built_in">return</span> <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">    ...</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>只用表达式，不用语句</li>
</ul>
<blockquote>
<p>表达式是一个单纯的运算过程，总是有返回值；语句是执行某种操作，没有返回值。函数式变成要求，只使用表达式，不使用语句。也就是说，每一步都是单纯的运算，而且都有返回值。</p>
</blockquote>
<blockquote>
<p>假设我们写项目中，多处需要改变某个元素的背景色，我们可以这样封装下</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var ele = doucument.querySelector(<span class="string">'.test'</span>)</span><br><span class="line"><span class="keyword">function</span> setBackgroundColor(color) &#123;</span><br><span class="line">  ele.style.backgroundColor = color</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 多处使用</span><br><span class="line">setBackgroundColor(<span class="string">'red'</span>)</span><br><span class="line">setBackgroundColor(<span class="string">'#ccc'</span>)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>我们可以很明显的感受到，setBackgroundColor封装的仅仅只是一条语句。这并不是理想的效果。函数式变成期望一个函数有输入，也有输出。因此良好的习惯应该如下：</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> setBackgroundColor(ele, color) &#123;</span><br><span class="line">  ele.style.backgroundColor = color</span><br><span class="line">  <span class="built_in">return</span> color</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 多处使用</span><br><span class="line">var ele = document.querySelector(<span class="string">'.test'</span>)</span><br><span class="line">setBackgroundColor(ele, <span class="string">'red'</span>)</span><br><span class="line">setBackgroundColor(ele, <span class="string">'#ccc'</span>)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>了解这一点，可以让我们自己在封装函数的时候养成良好的习惯。</p>
</blockquote>
<ul>
<li>纯函数</li>
</ul>
<blockquote>
<p>想同的输入总会得到相同的输出，并且不会产生副作用的函数，就是纯函数</p>
</blockquote>
<blockquote>
<p>所谓副作用，指的是函数内部与外部互动(最典型的情况，就是修改全局变量的值)，产生运算意外的其他结果。</p>
</blockquote>
<blockquote>
<p>函数式变成强调没有副作用，意味着函数要保持独立，所有功能就是返回一个新的值，没有其他行为，尤其是不的修改外部变量的值。即：<code>只要是同样的参数传入，返回的结果一定是相等的</code>。</p>
</blockquote>
<blockquote>
<p>例如我们期望封装一个函数，能够得到传入数组的最后一项。那么可以通过下面两种方式来实现。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> getLast(arr) &#123;</span><br><span class="line">  <span class="built_in">return</span> arr[arr.length - 1]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> getLast_(arr) &#123;</span><br><span class="line">  retrun arr.pop()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var <span class="built_in">source</span> = [1, 2, 3, 4]</span><br><span class="line"></span><br><span class="line">var last = getLast(<span class="built_in">source</span>) // 返回结果4 原数组不变</span><br><span class="line">var last_ = getLast_(<span class="built_in">source</span>) // 返回结果4 原数据最后一项被删除</span><br></pre></td></tr></table></figure>

<blockquote>
<p>getLast与getLast_虽然同样能够获得数组的最后一项值，但是getLast_改变了原数组。而当原始数组被改变，那么当我们再次调用该方法时，得到的结果就会变得不一样。这样不可预测的封装方式，在我们看来是非常糟糕的。它会把我们的数据搞得非常混乱。在JavaScript原生支持的数据方法中，也有许多不纯的方法，我们在使用时需要非常警惕，我们要清晰的知道原始数据的改变是否会留下隐患。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">var <span class="built_in">source</span> = [1, 2, 3, 4, 5]</span><br><span class="line"></span><br><span class="line">sourec.slice(1, 3) // 纯函数 返回[2, 3] <span class="built_in">source</span>不变</span><br><span class="line">source.splice(1, 3) // 不纯的 返回[2, 3, 4] <span class="built_in">source</span>被改变</span><br><span class="line"></span><br><span class="line">source.pop() // 不纯的</span><br><span class="line">source.push(6) // 不纯的</span><br><span class="line">source.shift() // 不纯的</span><br><span class="line">source.unshift(1) // 不纯的</span><br><span class="line">source.reverse() // 不纯的</span><br><span class="line"></span><br><span class="line">// 我也不能短时间知道现在<span class="built_in">source</span>被改变成了什么样子，干脆重新约定一下</span><br><span class="line"><span class="built_in">source</span> = [1, 2, 3, 4, 5]</span><br><span class="line"></span><br><span class="line">source.concat([6, 7]) // 纯函数 返回[1, 2, 3, 4, 5, 6, 7] <span class="built_in">source</span>不变</span><br><span class="line">source.join(<span class="string">'-'</span>) // 纯函数 返回1-2-3-4-5 <span class="built_in">source</span>不变</span><br></pre></td></tr></table></figure>
    </div>

    <div>
      
        <div>
  
    <div style="text-align:center;color: #ccc;font-size:20px;">------------- 本 文 结 束 <i class="fa fa-paw"></i> 感 谢 您 的 阅 读 -------------</div>
  
</div>
      
    </div>
    
    
    
    
        <div class="reward-container">
  <div>只是为了好玩罢了~</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="Archer 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="Archer 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Archer
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%85%AB)--%E5%87%BD%E6%95%B0%E4%B8%8E%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="前端基础进阶学习(八)--函数与函数式编程">https://www.chenkl.vip/前端基础进阶学习(八)--函数与函数式编程/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/javaScript/" rel="tag"><i class="fa fa-tag"></i> javaScript</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF%E8%BF%9B%E9%98%B6/" rel="tag"><i class="fa fa-tag"></i> 前端进阶</a>
              <a href="/tags/%E5%87%BD%E6%95%B0%E4%B8%8E%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" rel="tag"><i class="fa fa-tag"></i> 函数与函数式编程</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E4%B8%83)--this/" rel="prev" title="前端基础进阶学习(七)--this">
      <i class="fa fa-chevron-left"></i> 前端基础进阶学习(七)--this
    </a></div>
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E4%B9%9D)--%E6%B7%B1%E5%85%A5%E8%AF%A6%E8%A7%A3%E5%87%BD%E6%95%B0%E7%9A%84%E6%9F%AF%E9%87%8C%E5%8C%96/" rel="next" title="前端基础进阶学习(九)--深入详解函数的柯里化">
      前端基础进阶学习(九)--深入详解函数的柯里化 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#函数声明、函数表达式、匿名函数与自执行函数"><span class="nav-number">1.</span> <span class="nav-text">函数声明、函数表达式、匿名函数与自执行函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数声明"><span class="nav-number">1.1.</span> <span class="nav-text">函数声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#函数表达式"><span class="nav-number">1.2.</span> <span class="nav-text">函数表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名函数"><span class="nav-number">1.3.</span> <span class="nav-text">匿名函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数自执行与块级作用域"><span class="nav-number">2.</span> <span class="nav-text">函数自执行与块级作用域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数参数传递方式：按值传递"><span class="nav-number">3.</span> <span class="nav-text">函数参数传递方式：按值传递</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#函数式编程"><span class="nav-number">4.</span> <span class="nav-text">函数式编程</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Archer"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Archer</p>
  <div class="site-description" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">28</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">49</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kailong321200875" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;kailong321200875" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:502431556@qq.com" title="E-Mail → mailto:502431556@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/meng-shou-kong-cheng-43/activities" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;meng-shou-kong-cheng-43&#x2F;activities" rel="noopener" target="_blank"><i class="fa fa-fw fa-ioxhost"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-fas fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Archer</span>
  <span class="post-meta-divider">|</span>
  <span class="post-meta-item-icon">
    <i class="fa fa-area-chart"></i>
  </span>
  <span class="post-count">站点总字数：43.4k字</span>
</div>
  
  
  

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      访客数<span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>人次
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      总访问量<span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>次
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : 'e6f2f10abe388d591867',
      clientSecret: '580aea9e3da136ac529a71232367d1fdf3591926',
      repo        : 'blogTalk',
      owner       : 'kailong321200875',
      admin       : ['kailong321200875'],
      id          : '5236f111b9c97ef1378ebf96f88d72e1',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>

<!-- 页面鼠标点击样式 -->

  
    <script src="/js/src/fireworks.js"></script>
  





  <script src="https://cdn.jsdelivr.net/gh/kailong321200875/live2d-widget@1.0.1/autoload.js"></script>

